package com.longcai.piaojia.activity;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.adapter.BalanceDetailListAdapter;
import com.longcai.piaojia.conn.OrderBalanceDetailGet;
import com.longcai.piaojia.util.MyToast;
import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshListView;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.http.Http;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/10/17.
 */
public class BalanceDetailActivity extends BaseActivity {

    @BoundView(R.id.balance_detail_lv) private PullToRefreshListView pullToRefreshListView;

    private ListView balance_detail_lv;

    private List<OrderBalanceDetailGet.BalanceDetailEntity> lists = new ArrayList<>();
    private BalanceDetailListAdapter adapter;
    private OrderBalanceDetailGet.BalanceDetailListEntity balanceDetail;

    private OrderBalanceDetailGet orderBalanceDetailGet = new OrderBalanceDetailGet(new AsyCallBack<OrderBalanceDetailGet.BalanceDetailListEntity>(){
        @Override
        public void onSuccess(String toast, int type, OrderBalanceDetailGet.BalanceDetailListEntity balanceDetailListEntity) throws Exception {

            balanceDetail = balanceDetailListEntity;

            lists.addAll(balanceDetailListEntity.BalanceDetailList);

            adapter.notifyDataSetChanged();

            setLastUpdateTime();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.show(context, toast, Toast.LENGTH_SHORT);

        }

        @Override
        public void onEnd(String toast, int type) throws Exception {


            if(type==0){

                Http.getInstance().dismiss();

            }else{

                pullToRefreshListView.onPullDownRefreshComplete();
                pullToRefreshListView.onPullUpRefreshComplete();

            }

        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_balance_detail);

        setBackTrue();

        setTitleName(getString(R.string.deposit_right_title_text));

        pullToRefreshListView.setPullLoadEnabled(true);
        pullToRefreshListView.setPullRefreshEnabled(true);
        pullToRefreshListView.setScrollLoadEnabled(false);

        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                lists.clear();
                orderBalanceDetailGet.page = 1;
                orderBalanceDetailGet.user_id = BaseApplication.BasePreferences.readUserId();
                orderBalanceDetailGet.execute(BalanceDetailActivity.this, false, 1);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                if (balanceDetail != null && lists.size() != balanceDetail.total) {

                    orderBalanceDetailGet.page = balanceDetail.current_page + 1;
                    orderBalanceDetailGet.user_id = BaseApplication.BasePreferences.readUserId();
                    orderBalanceDetailGet.execute(BalanceDetailActivity.this, false, 1);

                } else {

                    pullToRefreshListView.onPullUpRefreshComplete();
                    pullToRefreshListView.onPullUpRefreshComplete();

                }

            }
        });

        balance_detail_lv = pullToRefreshListView.getRefreshableView();

        balance_detail_lv.setAdapter(adapter = new BalanceDetailListAdapter(context, lists));

        orderBalanceDetailGet.page = 1;
        orderBalanceDetailGet.user_id = BaseApplication.BasePreferences.readUserId();
        orderBalanceDetailGet.execute(this, false, 0);


    }


    private void setLastUpdateTime() {
        String text = formatDateTime(System.currentTimeMillis());
        pullToRefreshListView.setLastUpdatedLabel(text);
    }

    private String formatDateTime(long time) {
        SimpleDateFormat mDateFormat = new SimpleDateFormat("MM-dd HH:mm");

        if (0 == time) {
            return "";
        }
        return mDateFormat.format(new Date(time));
    }


}
